//
//  SISSEnrollHouseDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/17.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSEnrollHouseDetailViewController.h"
#import "SISSMorePopupView.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSJsonRequestNetworkingManager+Registration.h"
#import "SISSHouseResourceEnrollViewController.h"
#import "SISSNetworkingManager+Mine.h"
#import "SISSRecommendHouseViewController.h"

@interface SISSEnrollHouseDetailViewController ()<SISSMorePopupViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *neighbourhoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndDetailLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) IBOutlet UILabel *houseTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *listTagLabels;
@property (weak, nonatomic) IBOutlet UILabel *propertyStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *averagePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *buildYearLabel;
@property (weak, nonatomic) IBOutlet UILabel *decorationLabel;
@property (weak, nonatomic) IBOutlet UILabel *floorLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseCodingLabel;
@property (weak, nonatomic) IBOutlet UILabel *insideAreaLabel;
@property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;


@property (weak, nonatomic) IBOutlet UITextField *sellerNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *sellerPhoneNumTextField;
@property (weak, nonatomic) IBOutlet UILabel *insertTimeLabel;

@end

@implementation SISSEnrollHouseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (UILabel *label in self.listTagLabels) {
        
        label.hidden = YES;
    }

    // 添加左侧的返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    // 添加右侧分享删除按钮
    self.navigationItem.rightBarButtonItem = self.isChange ? [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)] : nil;
    
}

- (void)back {
    if (self.isChange) {
        if (self.deleteComplete) {
            self.deleteComplete();
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    } else {
        if (self.isRegistration) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else if (self.isCooperation) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSRecommendHouseViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
        }
    }
}

- (void)setHouseResourceModel:(SISSHouseResourceModel *)houseResourceModel {
    
    [self.view layoutIfNeeded];
    _houseResourceModel = houseResourceModel;
    
    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    
    for (int i = 0; i < houseResourceModel.listTag.count; i++) {
        UILabel *label = self.listTagLabels[i];
        label.hidden = NO;
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@" %@ ",houseResourceModel.listTag[i].tag];
        NSInteger colorIndex = houseResourceModel.listTag[i].color - 1;
        label.layer.borderColor = labelCololors[colorIndex].CGColor;
        label.textColor = labelCololors[colorIndex];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    self.neighbourhoodLabel.text = houseResourceModel.neighbourhood;
    self.regionAndDetailLabel.text = [NSString stringWithFormat:@"%@-%@",houseResourceModel.region,houseResourceModel.regionDetail];
    self.priceTextField.text = [NSString stringWithFormat:@"%0.f万",[houseResourceModel.price floatValue]];
    self.houseTypeLabel.text = houseResourceModel.houseType;
    self.areaLabel.text = [NSString stringWithFormat:@"%@㎡",[PublicUtil returnTwoDeciDecimalFormatter:houseResourceModel.area]];
    self.insideAreaLabel.text = [NSString stringWithFormat:@"%@㎡",[PublicUtil returnTwoDeciDecimalFormatter:houseResourceModel.insideArea]];
    self.propertyStateLabel.text = houseResourceModel.propertyState;
    self.averagePriceLabel.text = [NSString stringWithFormat:@"%.0f元/㎡",[houseResourceModel.averagePrice floatValue] * 10000];
    self.directionLabel.text = houseResourceModel.direction.length ? houseResourceModel.direction : @"暂无数据";
    self.buildYearLabel.text = houseResourceModel.year.length > 0 && [houseResourceModel.year integerValue] > 0 ? [NSString stringWithFormat:@"%@年",houseResourceModel.year] : @"暂无数据";
    self.decorationLabel.text = houseResourceModel.decoration.length ? houseResourceModel.decoration : @"暂无数据";
    self.floorLabel.text = houseResourceModel.floor ? [NSString stringWithFormat:@"%zd层",houseResourceModel.floor] : @"暂无数据";
    self.houseCodingLabel.text = houseResourceModel.houseInfoNo.length > 0 ? houseResourceModel.houseInfoNo : @"暂无数据";
    self.descriptionLabel.text = houseResourceModel.otherdesc.length ? houseResourceModel.otherdesc : @"暂无数据";
    
    
    self.sellerNameTextField.text = houseResourceModel.name;
    self.sellerPhoneNumTextField.text = houseResourceModel.phoneNumber;
    self.insertTimeLabel.text = [houseResourceModel.insertTime substringToIndex:10];
}


/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    if (self.houseResourceModel.traded == 1 || self.houseResourceModel.trading == 1) { // 已交易、交易中的只有分享
        self.isChange = NO;
    }
    NSArray *arr = self.isChange ? @[@"修改", @"删除", @"分享"] : @[@"分享"];
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:arr];
    moreView.delegate = self;
    [moreView show];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (!self.isChange) {
        
        [self share];
        return;
    }
    
    switch (index) {
       
        case 0: {
            SISSHouseResourceEnrollViewController *vc = [[SISSHouseResourceEnrollViewController alloc] init];
            vc.houseModel = self.houseResourceModel;
            vc.isModify = YES;
            vc.title = @"房源修改";
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
            
        case 1: {
            [self deleteHouse];
        }
            break;
            
        case 2: {
            [self share];
        }
            break;
         
        default:
            break;
    }
}

- (void)setAhrId:(NSString *)ahrId {
    _ahrId = ahrId;
    
    [kNetworkManager requestHouseResourceDetailWithHouseId:ahrId successBlock:^(SISSHouseResourceModel *houseModel) {
        
        self.houseResourceModel = houseModel;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

- (void)share {
    NSString *title = [NSString stringWithFormat:@"%@ | %@万",self.houseResourceModel.neighbourhood, self.houseResourceModel.price];
    NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",self.houseResourceModel.region,self.houseResourceModel.regionDetail,self.houseResourceModel.area,self.houseResourceModel.houseType];
    NSString *webpageUrl = @"";
    if (self.houseResourceModel.houseresType == 1) { // 线上
        webpageUrl = [NSString stringWithFormat:@"%@/?#!/sellerHouseDetail/%@",WEB_BASE_URL,self.houseResourceModel.ID];
    } else { // 线下
        webpageUrl = [NSString stringWithFormat:@"%@/#!/offlineHouseDetail/%@",WEB_BASE_URL,self.houseResourceModel.ID];
    }
    
    [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:self.houseResourceModel.houseImage.firstObject];
}



/**
 删除房源
 */
- (void)deleteHouse {
    
    [kNetworkManager deleteHouseResourceWithHouseId:self.houseResourceModel.ID
                                       successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 删除成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"删除成功！" andTime:1.0 andColor:YES];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.deleteComplete) {
                    self.deleteComplete();
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

@end
